//
//  TFristLoginUserInfoViewController.m
//  LoveShare
//
//  Created by ways on 2017/6/21.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TFristLoginUserInfoViewController.h"

#import "TUserInfoViewModel.h"
#import "TSelectListValueView.h"

@interface TFristLoginUserInfoViewController ()


@property (nonatomic,strong)TSelectListValueView *selectListValueView;
@property (nonatomic,strong)TUserInfoViewModel *userInfoViewModel;


@property (strong, nonatomic) IBOutlet UITextField *nickNameTextField;

@property (strong, nonatomic) IBOutlet UIButton *femaleButton;
@property (strong, nonatomic) IBOutlet UIButton *maleButton;
@property (strong, nonatomic) IBOutlet UIButton *areaSelectedButton;
@property (strong, nonatomic) IBOutlet UIButton *roomNumSelectedButton;

- (IBAction)submitButtonAction:(UIButton *)sender;

@end

@implementation TFristLoginUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initBind];
    [self refreshUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (TSelectListValueView *)selectListValueView {
    if (!_selectListValueView) {
        _selectListValueView = [[TSelectListValueView alloc] initWithFrame:CGRectMake((self.view.width - 250)/2, 0, 250, 0)];
        [self.view addSubview:_selectListValueView];
    }
    return _selectListValueView;
}

#pragma mark - init

- (TUserInfoViewModel *)userInfoViewModel {
    if (!_userInfoViewModel) {
        _userInfoViewModel = [[TUserInfoViewModel alloc] initUserInfoViewModel];
    }
    return _userInfoViewModel;
}


- (void)refreshUI {
    self.nickNameTextField.text = [TUserManager defaultManager].userInfoModel.result.nickname;
    if ([TUserManager defaultManager].userInfoModel.result.sex) {
        if ([[TUserManager defaultManager].userInfoModel.result.sex integerValue] == 0) {//女士
            self.femaleButton.selected = YES;
            [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
            [self.maleButton setImage:[UIImage imageNamed:@"user_sex_circle_normal"] forState:UIControlStateNormal];
            [self.femaleButton setImage:[UIImage imageNamed:@"user_sex_circle_selected"] forState:UIControlStateNormal];
        } else {//男士
            self.maleButton.selected = YES;
            [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
            [self.maleButton setImage:[UIImage imageNamed:@"user_sex_circle_selected"] forState:UIControlStateNormal];
            [self.femaleButton setImage:[UIImage imageNamed:@"user_sex_circle_normal"] forState:UIControlStateNormal];
        }
    }
    if ([[TUserManager defaultManager].userInfoModel.result.communityId integerValue] == 1) {//测试数据，待小区接口提供后修改
        [self.areaSelectedButton setTitle:@"岭南1" forState:UIControlStateNormal];
    }
    if ([TUserManager defaultManager].userInfoModel.result.roomNum) {
        [self.areaSelectedButton setTitle:[TUserManager defaultManager].userInfoModel.result.roomNum forState:UIControlStateNormal];
    }
    
}

- (void) initBind {
    
    @weakify(self)
    [[[self.nickNameTextField rac_textSignal] skip:1] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
    }];
    
    
    [[self.maleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        self.maleButton.selected = YES;
        [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
        [self.maleButton setImage:[UIImage imageNamed:@"user_sex_circle_selected"] forState:UIControlStateNormal];
        [self.femaleButton setImage:[UIImage imageNamed:@"user_sex_circle_normal"] forState:UIControlStateNormal];
        
        self.userInfoViewModel.userInfoModel.sex = @1;//男士
    }];
    [[self.femaleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        self.femaleButton.selected = YES;
        [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
        [self.maleButton setImage:[UIImage imageNamed:@"user_sex_circle_normal"] forState:UIControlStateNormal];
        [self.femaleButton setImage:[UIImage imageNamed:@"user_sex_circle_selected"] forState:UIControlStateNormal];
        
        self.userInfoViewModel.userInfoModel.sex = @0;//女士
    }];
    
    
    [[self.areaSelectedButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.view endEditing:YES];
        [self.selectListValueView selectListValueViewIsShow:YES withListValue:@[@"岭南1",@"岭南2",@"岭南3",@"岭南4"]];
        @weakify(self)
        _selectListValueView.selectedValueBlock = ^(NSString *value) {
            @strongify(self)
            self.areaSelectedButton.selected = YES;
            [self.areaSelectedButton setTitle:value forState:UIControlStateNormal];
            self.userInfoViewModel.userInfoModel.communityId = @1;//小区代码
        };
    }];
    
    
    [[self.roomNumSelectedButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.view endEditing:YES];
        [self.selectListValueView selectListValueViewIsShow:YES withListValue:@[@"A1001",@"A1002",@"A1003",@"A1004",@"A1005",@"A1006",@"A1007",@"A1008"]];
        @weakify(self)
        _selectListValueView.selectedValueBlock = ^(NSString *value) {
            @strongify(self)
            self.roomNumSelectedButton.selected = YES;
            [self.roomNumSelectedButton setTitle:value forState:UIControlStateNormal];
            
            self.userInfoViewModel.userInfoModel.roomNum = value;//房号
        };
    }];
    
    
}

#pragma mark - actions method

- (IBAction)submitButtonAction:(UIButton *)sender {
    
    if ([self.nickNameTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length != 0 &&
        (self.maleButton.selected || self.femaleButton.selected) &&
        self.areaSelectedButton.selected &&
        self.roomNumSelectedButton.selected) {//信息必须填写完整
        
        
    } else {
        [SVProgressHUD dismissWithDelay:1.0];
        [SVProgressHUD showErrorWithStatus:@"信息填写不完整"];
        return;
    }
    
    self.userInfoViewModel.userInfoModel.nickname = self.nickNameTextField.text;
    //self.userInfoViewModel.userInfoModel.sex = 点击事件设置
    //self.userInfoViewModel.userInfoModel.communityId = 点击事件设置
    //self.userInfoViewModel.userInfoModel.roomNum = 点击事件设置
    
    [[self.userInfoViewModel.requestCommand execute:nil] subscribeNext:^(TUserInfoModel  *_Nullable userInfoModel) {
        if ([userInfoModel.msgCode intValue] == 100) {//提交成功
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        } else {
            
        }
    }];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.selectListValueView selectListValueViewIsShow:NO withListValue:nil];
}

@end






